home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / site-packages / pythonloader.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  5KB  |  138 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import uno
  5. import unohelper
  6. import sys
  7. import imp
  8. import os
  9. from com.sun.star.uno import Exception, RuntimeException
  10. from com.sun.star.loader import XImplementationLoader
  11. from com.sun.star.lang import XServiceInfo
  12. MODULE_PROTOCOL = 'vnd.openoffice.pymodule:'
  13. DEBUG = 1
  14. g_supportedServices = ('com.sun.star.loader.Python',)
  15. g_implementationName = 'org.openoffice.comp.pyuno.Loader'
  16.  
  17. def splitUrl(url):
  18.     nColon = url.find(':')
  19.     if -1 == nColon:
  20.         raise RuntimeException('PythonLoader: No protocol in url ' + url, None)
  21.     
  22.     return (url[0:nColon], url[nColon + 1:len(url)])
  23.  
  24. g_loadedComponents = { }
  25.  
  26. def checkForPythonPathBesideComponent(url):
  27.     path = unohelper.fileUrlToSystemPath(url + '/pythonpath.zip')
  28.     if DEBUG == 1:
  29.         print 'checking for existence of ' + encfile(path)
  30.     
  31.     if 1 == os.access(encfile(path), os.F_OK) and path not in sys.path:
  32.         if DEBUG == 1:
  33.             print 'adding ' + encfile(path) + ' to sys.path'
  34.         
  35.         sys.path.append(path)
  36.     
  37.     path = unohelper.fileUrlToSystemPath(url + '/pythonpath')
  38.     if 1 == os.access(encfile(path), os.F_OK) and path not in sys.path:
  39.         if DEBUG == 1:
  40.             print 'adding ' + encfile(path) + ' to sys.path'
  41.         
  42.         sys.path.append(path)
  43.     
  44.  
  45.  
  46. def encfile(uni):
  47.     return uni.encode(sys.getfilesystemencoding())
  48.  
  49.  
  50. class Loader(XImplementationLoader, XServiceInfo, unohelper.Base):
  51.     
  52.     def __init__(self, ctx):
  53.         if DEBUG:
  54.             print 'pythonloader.Loader ctor'
  55.         
  56.         self.ctx = ctx
  57.  
  58.     
  59.     def getModuleFromUrl(self, url):
  60.         if DEBUG:
  61.             print 'pythonloader: interpreting url ' + url
  62.         
  63.         (protocol, dependent) = splitUrl(url)
  64.         if 'vnd.sun.star.expand' == protocol:
  65.             exp = self.ctx.getValueByName('/singletons/com.sun.star.util.theMacroExpander')
  66.             url = exp.expandMacros(dependent)
  67.             (protocol, dependent) = splitUrl(url)
  68.         
  69.         if DEBUG:
  70.             print 'pythonloader: after expansion ' + protocol + ':' + dependent
  71.         
  72.         
  73.         try:
  74.             if 'file' == protocol:
  75.                 url = unohelper.absolutize(url, url)
  76.                 mod = g_loadedComponents.get(url)
  77.                 if not mod:
  78.                     mod = imp.new_module('uno_component')
  79.                     checkForPythonPathBesideComponent(url[0:url.rfind('/')])
  80.                     filename = unohelper.fileUrlToSystemPath(url)
  81.                     fileHandle = file(filename)
  82.                     src = fileHandle.read().replace('\r', '')
  83.                     if not src.endswith('\n'):
  84.                         src = src + '\n'
  85.                     
  86.                     codeobject = compile(src, encfile(filename), 'exec')
  87.                     exec codeobject in mod.__dict__
  88.                     mod.__file__ = encfile(filename)
  89.                     g_loadedComponents[url] = mod
  90.                 
  91.                 return mod
  92.             elif 'vnd.openoffice.pymodule' == protocol:
  93.                 return __import__(dependent)
  94.             else:
  95.                 raise RuntimeException('PythonLoader: Unknown protocol ' + protocol + ' in url ' + url, self)
  96.         except ImportError:
  97.             e = None
  98.             raise RuntimeException("Couldn't load " + url + ' for reason ' + str(e), None)
  99.  
  100.  
  101.     
  102.     def activate(self, implementationName, dummy, locationUrl, regKey):
  103.         if DEBUG:
  104.             print 'pythonloader.Loader.activate'
  105.         
  106.         mod = self.getModuleFromUrl(locationUrl)
  107.         implHelper = mod.__dict__.get('g_ImplementationHelper', None)
  108.         if implHelper == None:
  109.             return mod.getComponentFactory(implementationName, self.ctx.ServiceManager, regKey)
  110.         else:
  111.             return implHelper.getComponentFactory(implementationName, regKey, self.ctx.ServiceManager)
  112.  
  113.     
  114.     def writeRegistryInfo(self, regKey, dummy, locationUrl):
  115.         if DEBUG:
  116.             print 'pythonloader.Loader.writeRegistryInfo'
  117.         
  118.         mod = self.getModuleFromUrl(locationUrl)
  119.         implHelper = mod.__dict__.get('g_ImplementationHelper', None)
  120.         if implHelper == None:
  121.             return mod.writeRegistryInfo(self.ctx.ServiceManager, regKey)
  122.         else:
  123.             return implHelper.writeRegistryInfo(regKey, self.ctx.ServiceManager)
  124.  
  125.     
  126.     def getImplementationName(self):
  127.         return g_implementationName
  128.  
  129.     
  130.     def supportsService(self, ServiceName):
  131.         return ServiceName in self.serviceNames
  132.  
  133.     
  134.     def getSupportedServiceNames(self):
  135.         return g_supportedServices
  136.  
  137.  
  138.